1592D - Hemose in ICPC - CodeForces Solution


binary search dfs and similar implementation interactive math number theory trees *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define N 502000

using namespace std;
struct edge{
    int to,next;
}e[N<<1];
const int mo=1000000007;
inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int n,head[N],cnt,c[N],tot;
pii ed[N];
void add(int u,int v){
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u,int fa){
    for (int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if (v==fa)continue;
        ed[++tot]=(pii){u,v};
        dfs(v,u);
    }
}
int ck(int l,int r){
    int sum=0;
    for (int i=1;i<=n;++i)c[i]=0;
    for (int i=l;i<=r;++i)c[ed[i].fi]=c[ed[i].se]=1;
    for (int i=1;i<=n;++i)sum+=c[i];
    cout<<"? "<<sum;
    for (int i=1;i<=n;++i)if (c[i])cout<<" "<<i;
    cout<<endl;
    int x;cin>>x;
    return x;
}
signed main(){
    n=read();int mx=0;
    for (int i=1;i<n;++i){
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    cout<<"? "<<n;
    for (int i=1;i<=n;++i)cout<<" "<<i;cout<<endl;
    cin>>mx;
    int l=1,r=n-1,res;
    while (l<=r){
        int mid=(l+r)>>1;
        if (ck(l,mid)==mx)r=mid-1,res=mid;
        else l=mid+1,res=mid+1;
    }
    cout<<"! "<<ed[res].fi<<" "<<ed[res].se<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers